package top.chukongxiang.project.base.oauth2.provider;

import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import top.chukongxiang.project.base.oauth2.UserDetailsServiceImpl;
import top.chukongxiang.project.base.oauth2.provider.cas.CasTokenGranter;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2022/1/6 14:45
 */
@Component("daoAuthenticationProvider")
public class MyDaoAuthenticationProvider extends DaoAuthenticationProvider {

    public MyDaoAuthenticationProvider(UserDetailsServiceImpl userDetailsService, PasswordEncoder passwordEncoder) {
        super();
        super.setUserDetailsService(userDetailsService);
        super.setPasswordEncoder(passwordEncoder);
    }


    /**
     * 重写Dao的support方法，江CasToken排除在外
     * @param authentication
     * @return
     */
    @Override
    public boolean supports(Class<?> authentication) {
        UserDetailsServiceImpl userDetailsService = (UserDetailsServiceImpl) super.getUserDetailsService();
        HttpServletRequest request = userDetailsService.getRequest();
        boolean equalsCasGrantType = CasTokenGranter.GRANT_TYPE.equals(request.getParameter(CasTokenGranter.GRANT_TYPE_PARAMETER_NAME));
        return super.supports(authentication) && !equalsCasGrantType;
    }
}
